//
//  HMComposeViewController.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/26.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit

class HMComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        prepareUI()

    }
    
    //设置ui
    private func prepareUI() {
        setUpNavigationBar()
        setupTextView()
        setupToolBar()
    }
    
    //设置textView
    private func setupTextView() {
        //添加控件
        view.addSubview(textView)
        
        //添加约束
        textView.snp_makeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    //设置toolbar 
    private func  setupToolBar() {
        //添加控件
        view.addSubview(toolBar)
        
        //添加 约束
        toolBar.snp_makeConstraints { (make) in
            make.leading.trailing.bottom.equalTo(view)
            make.height.equalTo(44)
        }
        
        //每个item对应的图片
        let itemSettings = [
            ["imageName": "compose_mentionbutton_background", "action": "mention"],
            ["imageName": "compose_emoticonbutton_background", "action": "emoticon"],
            ["imageName": "compose_trendbutton_background", "action": "trend"],
            ["imageName": "compose_toolbar_picture", "action": "picture"],
            ["imageName": "compose_add_background", "action": "add"]
        ]
        var items = [UIBarButtonItem]()
        var index = 0
        //遍历获取每一个items的信息
        for dict in itemSettings {
            //获取图片的名称
            let imageName = dict["imageName"]!
            //获取点击事件的方法
            let action = dict["action"]!
            
            //
            //创建uibarbuttonitem
            let  item  = UIBarButtonItem(imageName: imageName, target: self, action: Selector(action))
            items.append(item)
            
            let button = item.customView as! UIButton
            button.tag = index
            
            //添加弹簧效果item之间制动扩张
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
            index += 1
        }
    //删除最后一个弹簧
        items.removeLast()
        toolBar.items = items
    }
    //添加导航栏的子控件
    private func setUpNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(HMComposeViewController.close))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(HMComposeViewController.sendStatus))
    
        //右边按钮不可用
        navigationItem.rightBarButtonItem?.enabled = false
        setupTitleView()
    }
    
    //设置titleView
    private func setupTitleView() {
    
        let prefix = "发微博"
        
        if  let name = HMUserAccountViewModel.sharedInstance.userAccount?.screen_name {
            //有名字、\n换行
            let  title = prefix + "\n" + name
            
            //创建label
            let  titleLabel = UILabel()
            titleLabel.numberOfLines = 0
            titleLabel.textColor = UIColor.blackColor()
            titleLabel.textAlignment = NSTextAlignment.Center
            titleLabel.font = UIFont.systemFontOfSize(HMWelcomeLabelFontSize)
            //使用属性文本
            let attStringM = NSMutableAttributedString(string: title)
            let  range = (title as NSString).rangeOfString(name)
            //添加属性
//            NSForegroundColorAttributeName:文字的前景颜色
            attStringM.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: range)
            attStringM.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: range)
            
            titleLabel.attributedText = attStringM
            //label的size会根据label里面的文本内容自己计算
            titleLabel.sizeToFit()
            navigationItem.titleView  = titleLabel
       
        } else {
            
            //没有名字
            navigationItem.title = prefix
        }
        
    }
    
    
//MARK: 按钮点击事件
    //uibarbuttonitem的点击事件
    @objc private func picture() {
    print("点击了图片")
    }
    
    @objc private func trend() {
        print("#")
    }
    @objc private func mention() {
        print("mnetion")
    }
    @objc private func emoticon() {
        print("点击了表情")
    }
    @objc private func add() {
        print("点击了➕")
    }
    
    @objc private func didClickItem(button: UIButton) {
        switch button.tag {
        case 0:
            print("点击了图片")
        case 1:
            print("#")
        case 2:
            print("mnetion")
        case 3:
            print("点击了表情")
        case 4:
            print("点击了➕")
        default:
            print("没有按钮点击")
        }
    }
    //导航栏item的点击事件
    
    @objc private func close() {
    
        dismissViewControllerAnimated(true, completion: nil)
    }
    @objc private func sendStatus() {
    
        HMPrint("发送")
    }
    
//MARK:懒加载
    //键盘工具条
    private lazy var toolBar: UIToolbar = UIToolbar()
    //文本输入框
    private lazy var textView: HMPlaceholderTextView = {
        let tv = HMPlaceholderTextView()
        
        //设置
        tv.font = UIFont.systemFontOfSize(18)
        tv.textColor = UIColor.blackColor()
        tv.placeholder = "我是占位文本"
        tv.delegate = self
        return tv
    } ()
    
    
}

extension HMComposeViewController: UITextViewDelegate {
    
    func textViewDidChange(textView: UITextView) {
        //设置导航栏右边按钮的可用
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
}